package conf

import "github.com/BurntSushi/toml"
import (
	"flag"
	"time"
)

var (
	confPath string
	Conf *Config
)

func init()  {
	flag.StringVar(&confPath, "conf", "example.toml", "default config path")
}
func Init() (err error)  {
	Conf = Default()
	_, err = toml.DecodeFile(confPath,&Conf)
	return
}
func Default() *Config {
	return &Config{
		ReqStep: 10,
		RPCServer: &RPCServer{
			Network:           "tcp",
			Addr:              "3119",
			Timeout:           Duration(time.Second),
			IdleTimeout:       Duration(time.Second * 60),
			MaxLifeTime:       Duration(time.Hour * 2),
			ForceCloseWait:    Duration(time.Second * 20),
			KeepAliveInterval: Duration(time.Second * 60),
			KeepAliveTimeout:  Duration(time.Second * 20),
		},
	}
}

type Config struct {
	Redis *Redis
	MySQL *MySQL
	RPCServer *RPCServer
	Kafka *Kafka
	ReqStep int
	BizType []int32
}
type MySQL struct {
	UserName  string
	Password  string
	Network string
	Addr string
	DbName    string
	MaxIdle int
	MaxOpen int
}
type Redis struct {
	Network string
	Addr string
	Auth string
	Active int
	Idle int
	DialTimeout Duration
	ReadTimeout Duration
	WriteTimeout Duration
	IdleTimeout Duration
}

type RPCServer struct {
	Network           string
	Addr              string
	Timeout           Duration
	IdleTimeout       Duration
	MaxLifeTime       Duration
	ForceCloseWait    Duration
	KeepAliveInterval Duration
	KeepAliveTimeout  Duration
}
type Kafka struct {
	Brokers []string
}
type Duration time.Duration

// UnmarshalText unmarshal text to duration.
func (d *Duration) UnmarshalText(text []byte) error {
	tmp, err := time.ParseDuration(string(text))
	if err == nil {
		*d = Duration(tmp)
	}
	return err
}